package com.captain.special.栈队列;

import java.util.LinkedList;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/4/6 23:13
 */
public class 删除字符串中的所有相邻重复项1047 {
    public static void main(String[] args) {
        new 删除字符串中的所有相邻重复项1047().removeDuplicates(
                "abbaca");
    }

    public String removeDuplicates(String s) {
        StringBuilder stack = new StringBuilder();
        int top = -1;
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (top >= 0 && stack.charAt(top) == ch) {
                stack.deleteCharAt(top);
                top--;
            } else {
                stack.append(ch);
                top++;
            }
        }
        return stack.toString();
    }
}
